﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;

/// <summary>
/// Summary description for FOUtility
/// </summary>
namespace ThapCam.Ulti
{
    public static class TCUtility
    {
        public static int convertBooleanToInteger(bool value)
        {
            return value ? 1 : 0;
        }

        public static string formatToCurrency(double price)
        {
            return String.Format("{0:0,0} đ", price);
        }

        public static string formatNumber(double price)
        {
            return String.Format("{0:0,0}", price);
        }

        public static string formatRowNumber(int numRow)
        {
            return String.Format("{0:000}", numRow);
        }

        public static bool convertStringToBoolean(string value)
        {
            value = value.ToLower();
            if (value == "1" || value == "true")
                return true;
            return false;
        }

        public static string convertBooleanToString(bool value)
        {
            return value == true ? "1" : "0";
        }

        public static string convertDateToString(DateTime date)
        {
            if (date != null)
                return String.Format("{0:yyyy-MM-dd HH:mm:ss}", date);
            return "";
        }

        public static DateTime convertStringToDateTime(string data)
        {
            return DateTime.Parse(data);
        }

        public static string randomString(int length)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            String randomStr = "";
            int randNumber;
            for (int i = 0; i < length; i++)
            {
                if ((random.Next(1, 100) % 2) == 0)
                    randNumber = random.Next(97, 123); //char {a-z}
                else
                    randNumber = random.Next(48, 58); //int {0-9}

                randomStr = randomStr + (char)randNumber;
            }
            return randomStr;
        }

        public static string randomNumberString(int length)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            String randomStr = "";
            int randNumber;
            for (int i = 0; i < length; i++)
            {
                randNumber = random.Next(48, 58); //int {0-9}
                randomStr = randomStr + (char)randNumber;
            }
            return randomStr;
        }

        public static string clearUnicode(string accented)
        {
            if (accented == null || accented == "")
                return "";
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = accented.Normalize(NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public static string optimzeUrl(String url)
        {
            String value = clearUnicode(url);
            if (String.IsNullOrEmpty(value))
                return "";
            else
                return HttpUtility.UrlEncode(Regex.Replace(value, "[^a-zA-Z0-9_]+", "-", RegexOptions.Compiled));

        }

        public static bool isEmail(string email)
        {
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);
            return match.Success;
        }
    }
}